<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="专家名称" prop="scholarsName">
        <el-input v-model="queryParams.scholarsName" placeholder="请输入专家名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:expert:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:expert:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:expert:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:expert:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="expertList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="专家ID" align="center" prop="id" /> -->
      <el-table-column label="专家名称" align="center" prop="scholarsName">
        <template slot-scope="scope">
          <span style="color: red" v-if="scope.row.isTop == '1'">[顶] </span>
          <span style="color: red" v-if="scope.row.isBottom == '1'">[底] </span>
          <span>{{ scope.row.scholarsName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工作单位" align="center" prop="employer" />
      <el-table-column label="照片" align="center" prop="photos" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.photos" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="专家简介" align="center" prop="introduction" />
      <el-table-column label="发布状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.publish_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:expert:edit']">修改</el-button>
          <el-tooltip class="item" effect="light" content="上移" placement="top">
            <el-button size="mini" type="primary" icon="el-icon-top" @click="moveUpOrDown(scope.row, 'up')"
              v-hasPermi="['system:expert:edit']"></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="light" content="下移" placement="top">
            <el-button size="mini" type="primary" icon="el-icon-bottom" @click="moveUpOrDown(scope.row, 'down')"
              v-hasPermi="['system:expert:edit']"></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="light" :content="scope.row.isTop != '1' ? '置顶' : '取消置顶'" placement="top">
            <el-button size="mini" type="primary" :icon="scope.row.isTop != '1'
                ? 'el-icon-upload2'
                : 'el-icon-refresh-left'
              " @click="pinToTopOrEnd(scope.row, 'top')" v-hasPermi="['system:expert:edit']"></el-button>
          </el-tooltip>

          <el-tooltip class="item" effect="light" :content="scope.row.isBottom != '1' ? '置底' : '取消置底'" placement="top">
            <el-button size="mini" type="primary" :icon="scope.row.isBottom != '1'
                ? 'el-icon-download'
                : 'el-icon-refresh-left'
              " @click="pinToTopOrEnd(scope.row, 'end')" v-hasPermi="['system:expert:edit']"></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="light" content="删除" placement="top">
            <el-button size="mini" style="width: 22px" type="danger" icon="el-icon-delete"
              @click="handleDelete(scope.row)" v-hasPermi="['system:expert:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改学者风采对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="专家名称" prop="scholarsName">
          <el-input v-model="form.scholarsName" placeholder="请输入专家名称" />
        </el-form-item>
        <el-form-item label="工作单位" prop="employer">
          <el-input v-model="form.employer" placeholder="请输入工作单位" />
        </el-form-item>
        <el-form-item label="照片" prop="photos">
          <image-upload v-model="form.photos" :limit="1" :isShowSuggest="true" :imageWidth="imageSuggest" />
        </el-form-item>
        <el-form-item label="专家简介" prop="introduction">
          <el-input v-model="form.introduction" type="textarea" placeholder="请输入内容" maxlength="100" show-word-limit
            rows="4" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('0')">暂 存</el-button>
        <el-button @click="submitForm('1')">发 布</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listExpert,
  getExpert,
  delExpert,
  addExpert,
  updateExpert,
  moveUpOrDown,
  pinToTopOrEnd,
} from "@/api/bussiness/expert";

export default {
  name: "Expert",
  dicts: ["publish_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学者风采表格数据
      expertList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scholarsName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        scholarsName: [{ required: true, message: "专家名称不能为空", trigger: "blur" }],
        employer: [{ required: true, message: "工作单位不能为空", trigger: "blur" }],
        photos: [{ required: true, message: "照片不能为空", trigger: "blur" }],
        introduction: [{ required: true, message: "专家简介不能为空", trigger: "blur" }],
      },
      imageSuggest: "高250像素，宽420像素",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学者风采列表 */
    getList() {
      this.loading = true;
      listExpert(this.queryParams).then((response) => {
        this.expertList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        scholarsName: null,
        employer: null,
        photos: null,
        introduction: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学者风采";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getExpert(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学者风采";
      });
    },
    /** 提交按钮 */
    submitForm(status) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.status = status;
          if (this.form.id != null) {
            updateExpert(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExpert(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除学者风采编号为"' + ids + '"的数据项？')
        .then(function () {
          return delExpert(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/expert/export",
        {
          ...this.queryParams,
        },
        `expert_${new Date().getTime()}.xlsx`
      );
    },
    async moveUpOrDown(row, type) {
      let res = await moveUpOrDown({
        id: row.id,
        moveType: type,
      });
      if (res.code == 200) {
        this.getList();
        this.$modal.msgSuccess("移动成功");
      }
    },
    async pinToTopOrEnd(row, type) {
      let res = await pinToTopOrEnd({
        id: row.id,
        pinType: type,
      });
      if (res.code == 200) {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }
    },
  },
};
</script>
